﻿using System;
using System.Web;
using System.Windows;
using Core;
using MessageBox = System.Windows.MessageBox;
using OAuthBase = WeiBeeCommon.Core.OAuthBase;
using System.ComponentModel;
using System.Windows.Threading;
using System.Diagnostics;

namespace Pages
{
    public partial class Settings
    {
        private readonly OAuthBase _oAuth;
        public OAuthBase GetAuth()
        {
            return _oAuth;
        }

        public Settings(OAuthBase oauth) {
            InitializeComponent();
            _oAuth = oauth;
            _oAuth.SaveTokenAndTokenFunction = SaveTokenHelper.Saver;
            // Clean up token and tokensecret
            _oAuth.Token = string.Empty;
            _oAuth.TokenSecret = string.Empty;
            Title = @"登录" + _oAuth.Title;

        }

        void OpenAuthPage()
        {
            Action tempDelegate = new Action(() =>
            {
                DispatcherTimer timerOpenAuthenticationUrl = new DispatcherTimer();
                try
                {
                    string AuthenticationUrl = _oAuth.AuthorizationLinkGet();

                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        timerOpenAuthenticationUrl = new DispatcherTimer();
                        timerOpenAuthenticationUrl.Interval = TimeSpan.FromSeconds(3);
                        timerOpenAuthenticationUrl.Tick += new EventHandler((obj, args) =>
                        {
                            Process.Start(AuthenticationUrl);
                            timerOpenAuthenticationUrl.Stop();
                        });
                        timerOpenAuthenticationUrl.Start();
                    }
                    ));
                }
                catch (Exception ex)
                {
                    //LogHelper.LogError(ex);
                    MessageBox.Show(string.Format("无法连接到{0}微博，请稍后重试",_oAuth.Title));
                }
            }
                    );
            tempDelegate.BeginInvoke(null, null);
        }

        BackgroundWorker _bgwAccessTokenGet;
        private void BtnOkClick(object sender, RoutedEventArgs e) {
            try {
                _oAuth.Verifier = txtUpdate.Text.Trim();
                _bgwAccessTokenGet = new BackgroundWorker();
                _bgwAccessTokenGet.DoWork += new DoWorkEventHandler(_bgwAccessTokenGet_DoWork);
                _bgwAccessTokenGet.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_bgwAccessTokenGet_RunWorkerCompleted);
                _bgwAccessTokenGet.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        void _bgwAccessTokenGet_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.DialogResult = true;
            _oAuth.SaveTokenAndTokenSecret();
            Close();
        }

        void _bgwAccessTokenGet_DoWork(object sender, DoWorkEventArgs e)
        {
            _oAuth.AccessTokenGet(_oAuth.Token, _oAuth.Verifier);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtUpdate.Focus();
            OpenAuthPage();
        }
    }
}
